//
//  ViewController.m
//  飞机大战
//
//  Created by ZSY on 17/7/18.
//  Copyright © 2017年 ZSY. All rights reserved.
//

#import "ViewController.h"
#import "PlaneScene.h"
#import <SceneKit/SceneKit.h>

@interface ViewController () <PlaneDelegate> {
    SKView * skView;
    UILabel *lb_score;
}
@property(nonatomic,strong) CAGradientLayer * skyLayer;
@end
@implementation ViewController
- (CAGradientLayer *)skyLayer {
    if (!_skyLayer) {
        _skyLayer = [CAGradientLayer layer];
        _skyLayer.frame = self.view.bounds;
        UIColor *lightColor = [UIColor colorWithRed:40.0 / 255.0 green:150.0 / 255.0 blue:200.0 / 255.0 alpha:1.0];
        UIColor *darkColor = [UIColor colorWithRed:255.0 / 255.0 green:250.0 / 255.0 blue:250.0 / 255.0 alpha:1.0];
        _skyLayer.colors = @[(__bridge id)lightColor.CGColor,(__bridge id)darkColor.CGColor];
        _skyLayer.startPoint = CGPointMake(0, 0);
        _skyLayer.endPoint  = CGPointMake(1, 1);
    }
    return _skyLayer;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.view.layer addSublayer:self.skyLayer];
    skView = [[SKView alloc] initWithFrame:self.view.bounds];
    skView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:skView];

    lb_score = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 20)];
    lb_score.text = @"分数：0";
    lb_score.font = [UIFont systemFontOfSize:15];
    lb_score.textAlignment = NSTextAlignmentCenter;
    lb_score.textColor = [UIColor redColor];
    [skView addSubview:lb_score];
    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;
        // Create and configure the scene.
        PlaneScene *scene = [PlaneScene sceneWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
        scene.delegate = self;
        // Present the scene.
        [skView presentScene:scene];
    }
}
- (void) handleTap:(UIGestureRecognizer*)gestureRecognize {
    // retrieve the SCNView
    SCNView *scnView = (SCNView *)self.view;
    // check what nodes are tapped
    CGPoint p = [gestureRecognize locationInView:scnView];
    NSArray *hitResults = [scnView hitTest:p options:nil];
    // check that we clicked on at least one object
    if([hitResults count] > 0){
        // retrieved the first clicked object
        SCNHitTestResult *result = [hitResults objectAtIndex:0];
        // get its material
        SCNMaterial *material = result.node.geometry.firstMaterial;
        // highlight it
        [SCNTransaction begin];
        [SCNTransaction setAnimationDuration:0.5];
        // on completion - unhighlight
        [SCNTransaction setCompletionBlock:^{
            [SCNTransaction begin];
            [SCNTransaction setAnimationDuration:0.5];
            material.emission.contents = [UIColor blackColor];
            [SCNTransaction commit];
        }];
        material.emission.contents = [UIColor redColor];
        [SCNTransaction commit];
    }
}
-(void)gameOver:(SKScene *)scene {
    [scene removeFromParent];
    scene = nil;
    UIButton *btn = [[UIButton alloc] initWithFrame:self.view.frame];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn setTitle:@"游戏结束，点我开始" forState:0];
    [btn addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    [skView addSubview:btn];
}
- (void)getScore:(int)score {
    lb_score.text = [NSString stringWithFormat:@"分数:%d",score];
}
-(void)start:(UIButton *)btn {
    [btn removeFromSuperview];
    PlaneScene * scene = [PlaneScene sceneWithSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
    scene.delegate = self;
    lb_score.text = @"分数：0";
    // Present the scene.
    [skView presentScene:scene];
}
- (BOOL)shouldAutorotate {
    return YES;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
